﻿using System;
using System.Collections.Generic;
using System.Linq;
using Abp.Dependency;
using Abp.EntityFrameworkCore;
using FwProject.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Abp.Domain.Repositories;
using System.Threading.Tasks;
using FwProject.Comm.DapperPage;
using FwProject.PublicService.SqlExtensions;
using Mondol.DapperPoco;
using X.PagedList;

namespace FwProject.PublicService.SqlExec
{
    public class SqlExecuter:ISqlExecuter,ITransientDependency
    {
        private readonly IDbContextProvider<FwProjectDbContext> _dbContextProvider; 

        public SqlExecuter(IDbContextProvider<FwProjectDbContext> dbContextProvider){

            _dbContextProvider = dbContextProvider;
        }
        /// <summary>
        /// 执行sql
        /// </summary>
        /// <returns>The execute.</returns>
        /// <param name="sql">Sql.</param>
        /// <param name="parameters">Parameters.</param>
        public  int Execute(string sql, params object[] parameters){
            return _dbContextProvider.GetDbContext().Database.ExecuteSqlCommand(sql, parameters);        
        }
        /// <summary>
        /// 查询sql 执行
        /// </summary>
        /// <returns>The query.</returns>
        /// <param name="sql">Sql.</param>
        /// <param name="parameters">Parameters.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public IQueryable<T> FromSqlQuery<T>(string sql, Dictionary<string, object> Params) where T:class ,new()
        {
            return _dbContextProvider.GetDbContext().DynamicListFromSql<T>(sql, Params).AsQueryable();
        }
        /// <summary>
        /// dapper 同步方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="Params"></param>
        /// <returns></returns>
        public IList<T> FromDapperSqlQuery<T>(string sql, object Params) where T : class, new()
        {
            return _dbContextProvider.GetDbContext().DapperDynamicListFromSql<T>(sql, Params);
        }
        /// <summary>
        /// dapper异步方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="Params"></param>
        /// <returns></returns>
        public async Task<IEnumerable<T>>  FromDapperAsyncSqlQuery<T>(string sql, object Params) where T : class, new()
        {
            return await _dbContextProvider.GetDbContext().DapperAsyncDynamicListFromSql<T>(sql, Params);
        }

        public Paged<T> DapperPageDynamicListFromSql<T>(string sql, string where="", int page = 1, int pageSzie = 10, string PrimaryKey = "Id") where T : class, new()
        {
            return _dbContextProvider.GetDbContext().DapperPageDynamicListFromSql<T>(sql, where,  page ,  pageSzie,  PrimaryKey = "Id");
        }

        public IPagedList<T> IPagedListDynamicListFromSql<T>(string sql, string where = "", int page = 1, int pageSzie = 10, string PrimaryKey = "Id") where T : class, new()
        {
            Paged<T> dapperPage = _dbContextProvider.GetDbContext().DapperPageDynamicListFromSql<T>(sql, where, page , pageSzie, PrimaryKey = "Id");
            IPagedList<T> pagedList = new StaticPagedList<T>(dapperPage.Items,dapperPage.CurrentPage,dapperPage.ItemsPerPage,(int)dapperPage.TotalItems);
            return pagedList;
        }

        public async Task<Paged<T>> DapperPageAsyncDynamicListFromSql<T>(string sql, string where="", int page = 1, int pageSzie = 10, string PrimaryKey = "Id") where T : class, new()
        {
            return await _dbContextProvider.GetDbContext().DapperPageAsyncDynamicListFromSql<T>(sql, where, page , pageSzie , PrimaryKey = "Id");
        }

        public async Task<IPagedList<T>> IPagedListDynamicListFromSqlAsync<T>(string sql, string where = "", int page = 1, int pageSzie = 10, string PrimaryKey = "Id") where T : class, new()
        {
            Paged<T> dapperPage = await _dbContextProvider.GetDbContext().DapperPageAsyncDynamicListFromSql<T>(sql, where, page, pageSzie, PrimaryKey = "Id");
            IPagedList<T> pagedList = new StaticPagedList<T>(dapperPage.Items, dapperPage.CurrentPage, dapperPage.ItemsPerPage, (int)dapperPage.TotalItems);
            return pagedList;
        }


        public IQueryable<T> SqlQuery<T>(string sql, params object[] parameters) where T:class ,new()
        {
            return _dbContextProvider.GetDbContext().Set<T>().FromSql(sql, null).AsQueryable();
        }
      
    }
}
